﻿@model Controller2.Models.Job
@{
    Layout = null;
}
<!DOCTYPE html>
<html class="no-js">
<head>
    <title>Cadastro</title>
    @(Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => group.Add("telerik.common.css").Add("telerik.vista.css").Combined(true).Compress(true)))
    @if (false)
    {
        <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
        <link href="@Url.Content("~/Content/themes/controller/controller.css")" rel="stylesheet" type="text/css" />
        <script src="@Url.Content("~/Scripts/jquery.min.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/Scripts/jquery-ui.js")" type="text/javascript"></script>    
        <script src="@Url.Content("~/Scripts/jquery.maskedinput-1.3.min.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
    }
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/controller.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/numbers.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
    <style type="text/css">
        .display-label, .editor-label
        {
            width: 100px;
            text-align: right;
            padding-right: 5px;
        }
        #Status
        {
            width: 170px;
        }
        #NomeCliente
        {
            width: 343px;
        }
        #IdCliente
        {
            width: 50px;
            text-align: right;
        }
        #IdFuncionario, #IdFreelancer, #IdCampanha, #IdMidia, #Etapa
        {
            width: 400px;
        }
        textarea#Descricao
        {
            width: 400px;
        }
        #btnCliente, #btnFunc, #btnFreela
        {
            height: 21px;
            margin: 0;
            padding: 0 5px 20px 5px;
            line-height: 21px;
            vertical-align: top;
        }
    </style>
    @if (false)
    {
        <script src="../../Scripts/jquery-vsdoc.js" type="text/javascript"></script>
        <script src="../../Scripts/jquery-ui.js" type="text/javascript"></script>
        <script src="../../Scripts/controller.js" type="text/javascript"></script>
    }
</head>
<body id="frame">
    <div id="formHide">
        @using (Html.BeginForm("Salvar", "Job"))
        {
            @Html.ValidationSummary(true)
            <fieldset>
                <legend>Job</legend>
                @Html.HiddenFor(model => model.IdJob)
                <div class="editor-label">
                    @Html.LabelFor(model => model.DataCadastro, "Data Cadastro: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.DataCadastro)
                    @Html.ValidationMessageFor(model => model.DataCadastro)
                    <label for="DataInicio" style="margin-left: 137px;">
                        Data Início:
                    </label>
                    @Html.EditorFor(model => model.DataInicio)
                    @Html.ValidationMessageFor(model => model.DataInicio)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Status, "Status: ")
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.Status, (IEnumerable<SelectListItem>)ViewBag.Status, new { @class = "obrigatorio " })
                    @Html.ValidationMessageFor(model => model.Status)
                    <label for="DataConclusao" style="margin-left: 25px;">
                        Data Conclusão:
                    </label>
                    @Html.EditorFor(model => model.DataConclusao)
                    @Html.ValidationMessageFor(model => model.DataConclusao)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.IdCliente, "Cliente: ")
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(model => model.IdCliente, new { @readonly = "readonly" })
                    @Html.TextBox("NomeCliente", Model.Cliente != null ? Model.Cliente.NomePessoa : string.Empty, new { @readonly = "readonly" })
                    @Html.ValidationMessageFor(model => model.IdCliente)
                    @if (!User.IsInRole("Cliente"))
                    {
                        <input type="button" id="btnCliente" value="..." />
                    }
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.IdFuncionario, "Funcionário: ")
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.IdFuncionario, (IEnumerable<SelectListItem>)ViewBag.Funcionario, string.Empty)
                    @Html.ValidationMessageFor(model => model.IdFuncionario)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.IdFreelancer, "Freelancer: ")
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.IdFreelancer, (IEnumerable<SelectListItem>)ViewBag.Freelancer, string.Empty)
                    @Html.ValidationMessageFor(model => model.IdFreelancer)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.IdCampanha, "Campanha: ")
                </div>
                <div class="editor-field">
                    @if (Model.IdCliente > 0)
                    {
                        @Html.DropDownListFor(model => model.IdCampanha, (IEnumerable<SelectListItem>)ViewBag.Campanha, string.Empty, new { @class = "obrigatorio" })
                    }
                    else
                    {
                        @Html.DropDownListFor(model => model.IdCampanha, (IEnumerable<SelectListItem>)ViewBag.Campanha, string.Empty, new { @class = "obrigatorio", disabled = "disabled" })
                    }
                    @Html.ValidationMessageFor(model => model.IdCampanha)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Etapa, "Etapa: ")
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.Etapa, (IEnumerable<SelectListItem>)ViewBag.Etapa, "", new { @class = "obrigatorio" })
                    @Html.ValidationMessageFor(model => model.Etapa)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.IdMidia, "Mídia")
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.IdMidia, (IEnumerable<SelectListItem>)ViewBag.Midia, String.Empty, new { @class = "obrigatorio " })
                    @Html.ValidationMessageFor(model => model.IdMidia)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Descricao, "Descrição: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Descricao)
                    @Html.ValidationMessageFor(model => model.Descricao)
                </div>
                @if (!User.IsInRole("Cliente"))
                {
                    <div class="editor-label">
                        &nbsp;
                    </div>
                    <div class="editor-field">
                        @Html.CheckBoxFor(model => model.GerarContaReceber)
                        @Html.LabelFor(model => model.GerarContaReceber, "Gerar lançamento no Contas a Receber")
                    </div>
                }
            </fieldset>        
            <div id="botoesForm">
                @if (!User.IsInRole("Cliente"))
                {
                    <input type="submit" id="Gravar" value="Gravar" />
                }
                <input type="button" id="Fechar" value="Cancelar" />
            </div>
        }
    </div>
    <div id="alertaErro" style="display: none">
    </div>
    <div id="winFormPesquisaCliente" style="display: none">
    </div>
    <div id="winFormPesquisaFuncionario" style="display: none">
    </div>
    <div id="winFormPesquisaFreelancer" style="display: none">
    </div>
    @if (User.IsInRole("Cliente"))
    {        
        <script type="text/javascript">
            $(function () {
                $('#formHide').fadeIn('slow');
                $('.datepicker').mask("99/99/9999");
                hideLoading();
                $('#IdCampanha, #IdMidia, #GerarContaReceber, #IdFuncionario, #IdFreelancer').attr('disabled', 'disabled');
                $('#Status').val('@Model.Status').attr('disabled', 'disabled');
                $('#Etapa').val('@Model.Etapa').attr('disabled', 'disabled');
            });
            $('#Fechar').click(function () { closeWindow(); });            
        </script>
    }
    else
    {
        <script type="text/javascript">
            // custom validate para campos numéricos
            $(function () {
                if ($('#IdCliente').val() == 0) {
                    $('#IdCliente').val('');
                }
                if ($('#IdFuncionario').val() == 0) {
                    $('#IdFuncionario').val('');
                }
                if ($('#IdFreelancer').val() == 0) {
                    $('#IdFreelancer').val('');
                }
                $('.datepicker').each(function () {
                    $(this).rules("add", {
                        dataValidate: true,
                        validaDataMaior: true,
                        messages: {
                            dataValidate: "*",
                            validaDataMaior: "*"
                        }
                    });
                });
                $('.datepicker').mask("99/99/9999");
                textarea_maxlength("Descricao", "1000");
                $('#formHide').fadeIn('slow');
                hideLoading();
                if ('@Model.IdJob' > 0) {
                    $('#IdCampanha, #IdMidia, #GerarContaReceber').attr('disabled', 'disabled');
                    $('#btnCliente').hide();
                    $('#Status').val('@Model.Status');
                    $('#Etapa').val('@Model.Etapa');
                }
                $('#Nome').focus();
            });
            $('form').submit(function (e) {
                e.preventDefault();
                if ($('form').valid()) {
                    $.post('@Url.Action("Salvar", "Job")', $('form').serialize(), function (data) {
                        if (data.salvo) {
                            closeWindow();
                        } else {
                            alertError(data.error);
                        }
                        $('#gravar').attr('disabled', false);
                    });
                }
            });
            $.validator.methods.dataValidate = function (value, element, param) {
                return validaData(element);
            };
            $.validator.methods.validaDataMaior = function (value, element, param) {
                return validaDataMaior($("#DataCadastro"), $("#DataInicio")) && validaDataMaior($("#DataCadastro"), $("#DataConclusao")) && validaDataMaior($("#DataInicio"), $("#DataConclusao"));
            };
            $('#Fechar').click(function () { closeWindow(); });
            function carregaComboCampanha(idCliente) {
                showLoading();
                if (idCliente == "") {
                    $('#IdCampanha').empty();
                    $('#IdCampanha').attr('disabled', 'disabled');
                } else {
                    $('#IdCampanha').empty();
                    $('#IdCampanha').append('<option>Carregando...</option>');
                    var url = '@Url.Action("CarregarComboCampanha", "Job")';
                    $.post(url, { idCliente: idCliente }, function (dados) {
                        $('#IdCampanha').empty();
                        $('#IdCampanha').append('<option value=""></option>');
                        $.each(dados, function (index, campanha) {
                            $('#IdCampanha').append('<option value="' + campanha.Value + '">' + campanha.Text + '</option>');
                        });
                        $('#IdCampanha').removeAttr('disabled');
                    });
                }
                hideLoading();
            }
            $('#btnCliente').click(function () {
                showLoading(); var url = '@Url.Action("PesquisaCliente", "Cliente")'; $('#winFormPesquisaCliente').empty().load(url); formDialog('winFormPesquisaCliente', 'Pesquisa de Clientes', 460, 700); return false;
            });
            function preencherCamposPesquisaCliente(idCliente) {
                $.post('@Url.Action("BuscaNomeCliente", "Cliente")', { id: idCliente }, function (data) {
                    $('#IdCliente').val(idCliente);
                    $('#NomeCliente').val(data);
                    carregaComboCampanha(idCliente);
                });
                hideLoading();
            }
            function closeWindowPesquisa() {
                $('#winFormPesquisaCliente').dialog('close');
            }
        </script>
    }
</body>
</html>
